package com.yanqu.collectclientdata.mgr;

import com.yanqu.collectclientdata.controller.ClientLog;
import com.yanqu.collectclientdata.db.business.ClientLogBusiness;
import org.apache.commons.collections.CollectionUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author wpj
 * @Date 2020/11/30
 */
public class ClientLogMgr{
    public static List<ClientLog> clientLogList = new ArrayList<>();

    private static final Logger logger = LogManager.getLogger(ClientLogMgr.class.getName());

    public static void addClientLog(ClientLog clientLog){
        synchronized (clientLogList) {
            clientLogList.add(clientLog);
        }
    }

    public static void save() {
        if (!CollectionUtils.isEmpty(clientLogList)) {
            List<ClientLog> list;
            synchronized (clientLogList) {
                list = new ArrayList<>(clientLogList);
                clientLogList.clear();
                logger.error("save size : {}", list.size());
            }
            ClientLogBusiness.addClientLog(list);
        }
    }
}
